<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Service\FlyShopFansService;
use Hyperf\Validation\Rule;
use Psr\Http\Message\ServerRequestInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;

class StarController extends AbstractController
{

    /**
     * @Inject
     * @var FlyShopFansService
     */
    private $fans;


    public function star_num()
    {
      return $this->jsonResponse->success( $this->fans->getStarNum(intval($this->getAdminId())),'粉丝数量');
    }
    public function list()
    {
        $key_word = $this->request->input('key_word','');
        $list = $this->paginate($this->fans->getStar(intval($this->getAdminId()),$key_word,1,2))->toArray();
        if($list['data']!=[]){
            foreach ($list['data'] as $key=>$v){
                $list['data'][$key]['shop_fans'] = $this->fans->getShopFans(intval($v['shop_id']));
            }
        }
        return $this->jsonResponse->success($list,'关注列表');
    }

    public function add()
    {
        $params = $this->request->inputs(['shop_id','type','resource_type','resource_id']);
        $params['user_id']=$this->getAdminId();
        $this->validation($params,[
            'type'=>'required|max:5|min:1',
            'resource_type'=>'required|max:2|min:1',
        ]);
        $validator = $this->validationFactory->make($params,[],[]);
        $validator->sometimes('resource_id', ['resource_id'=>[
            'integer',
            'min:1',
            'required',
        ],],function($inputs) {
            return $inputs->resource_type != 1;
        });
        $validator->sometimes('shop_id', ['resource_id'=>[
            'integer',
            'min:1',
            'required',
            'exists:fly_shop_fans,id',
            Rule::unique('fly_shop_fans','shop_id')
                ->where('user_id',$params['user_id'])
                ->where('type',$params['type'])
                ->where('resource_type',$params['resource_type'])
                ->where('shop_id',$params['shop_id'])
        ],],function($inputs) {
            return $inputs->resource_type == 1;
        });

        if ($validator->fails()) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, $validator->errors()->first());
        }
        Db::beginTransaction();
        $res = $this->fans->addFans($params);
        if($res){
            Db::commit();
            return $this->jsonResponse->success($res,'添加成功');
        }else{
            Db::rollBack();
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'添加失败');
        }

    }

    public function cancel()
    {
        $params = $this->request->inputs(['link_id','type','resource_type']);
        $this->validation($params,[
            'type'=>'required|max:5|min:1',
            'resource_type'=>'required|max:2|min:1',
            'resource_id'=>Rule::requiredIf($params['resource_type']==1),
        ]);
        Db::beginTransaction();
        $res = $this->fans->cancelFans(intval($this->getAdminId()),intval($params['link_id']),intval($params['type']),intval($params['resource_type']));
        if($res){
            Db::commit();
            return $this->jsonResponse->success($res,'取消成功');
        }else{
            Db::rollBack();
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'取消失败');
        }

    }
}
